package com.qfjy.project.weixin.api.accessToken;

import com.qfjy.project.weixin.main.MenuManager;
import com.qfjy.project.weixin.util.WeixinUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
@Slf4j
public class AccessTokenThread extends Thread{
    /**
     * 微信调用凭据
     * https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
     */
    public static String ACCESS_TOKEN_VALUE;
    @Override
    public void run() {
        while (true){
            ACCESS_TOKEN_VALUE=this.sendWeiXinApi();
            System.out.println("生成的:"+ACCESS_TOKEN_VALUE);
            try {
                Thread.sleep(1000*5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
    private static String WEIXIN_API_GET_ACEESS_TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";


    private String sendWeiXinApi(){
        String url=WEIXIN_API_GET_ACEESS_TOKEN_URL.replace("APPID", MenuManager.appId).replace("APPSECRET",MenuManager.appSecret);
        JSONObject jsonObject = WeixinUtil.httpRequest(url, "GET", null);
        return jsonObject.getString("access_token");
    }
}